/*
 * DeleteAddressServlet.java
 * 作用：处理用户删除收货地址的请求。
 */
package com.campustradingwall.controller.user;

import com.campustradingwall.dao.AddressDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/deleteAddress")
public class DeleteAddressServlet extends HttpServlet {
    private AddressDao addressDao = new AddressDao();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int addressId = Integer.parseInt(request.getParameter("id"));
            // 在实际应用中，还应验证该地址是否属于当前登录用户，以防越权删除
            addressDao.deleteAddress(addressId);
            request.getSession().setAttribute("message", "地址删除成功！");
        } catch (NumberFormatException e) {
            request.getSession().setAttribute("message", "删除失败：无效的地址ID。");
            e.printStackTrace();
        }
        response.sendRedirect(request.getContextPath() + "/user/profile");
    }
} 